package com.pai.servlet;

import com.alibaba.fastjson.JSON;
import com.pai.entity.AppUser;
import com.pai.entity.Personnel;
import com.pai.service.AppUser.AppUserService;
import com.pai.service.AppUser.AppUserServiceImpl;
import com.pai.service.Department.DepartmentService;
import com.pai.service.Department.DepartmentServiceImpl;
import com.pai.service.Personnel.PersonnelService;
import com.pai.service.Personnel.PersonnelServiceImpl;
import com.pai.service.Role.RoleService;
import com.pai.service.Role.RoleServiceImpl;
import com.pai.vo.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 辅导员管理api
 */
@WebServlet(name = "persCounselorServlet",urlPatterns = {"/persCounselor"})
public class PersCounselorServlet extends BaseServlet{
    private PersonnelService theService;  //人员服务层
    private AppUserService appUserService;  //系统用户服务层
    private DepartmentService departmentService;  //部门服务层
    private RoleService roleService;  //角色服务层

    @Override
    public void init() throws ServletException {
        super.init();
        this.theService = new PersonnelServiceImpl();
        this.appUserService = new AppUserServiceImpl();
        this.departmentService = new DepartmentServiceImpl();
        this.roleService = new RoleServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();

        try{
            String action =req.getParameter("action");
            switch (action){
                case "list":
                    String departmentId = req.getParameter("departmentId");
                    List<Personnel> list = theService.getList(departmentId, null, null);
                    apiResult.setData(list);
                    break;
                case "detail":
                    String id = req.getParameter("id");
                    Personnel itemDetail = theService.getItemDetail(id);
                    apiResult.setData(itemDetail);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }

    //修改
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        Personnel item = JSON.parseObject(jsonString, Personnel.class);

        flag = theService.update(item);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("更新失败");
        }

        super.write(resp, apiResult);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String id = req.getParameter("id");

        flag = theService.delete(id);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("删除失败");
        }

        super.write(resp, apiResult);
    }

    //新增
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        Personnel item = JSON.parseObject(jsonString, Personnel.class);

        //创建一个辅导员的账号先
        //获得角色id
            int roleId = roleService.getItemByName("辅导员").getId();
            //为他创建账号
            AppUser _user = new AppUser();
            _user.setAccount(item.getWorkno());
            _user.setPassword("123456");  //默认密码123456
            _user.setAppRoleId(roleId);
            int _appUserId = appUserService.add(_user);  //创建好账号

            //把账号绑定到这个人身上去
            item.setAccountId(_appUserId);
            //然后才去新增
        try{
            flag = theService.add(item);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }
}
